import axios from "axios";

export class Request {
    constructor(baseurl, timeout = 6000) {
        const options = {
            baseURL: baseurl,
            timeout
        }
        this.instance = axios.create(options);
        this.interceptors(this.instance)
    }
    interceptors(instance) {
        instance.interceptors.request.use(this.requestUse, this.errorHandler)
        instance.interceptors.response.use(this.responseUse, this.errorHandler)
    }
    
    requestUse(config){
        return config
    }
    responseUse(response){
        return response.data
    }
    errorHandler(error) {
        console.error(error)
        return Promise.reject(error)
    }
    get(url, data) {
        return this.instance({
            url: url,
            params: data,
            method: 'get',
            headers: {
                'Content-Type': 'application/json'
            }
        })
    }
    post(url, data) {
        return this.instance({
            url: url,
            data: data,
            method: 'post',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
    }
    put() { }
    delete() { }
}


